package io.renren.modules.noc.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.noc.service.NocBusinessService;
import io.renren.modules.noc.service.NocMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.noc.entity.NocReadCardEntity;
import io.renren.modules.noc.service.NocReadCardService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-04-08 00:54:01
 */
@RestController
@RequestMapping("noc/nocreadcard")
public class NocReadCardController extends BaseController{
    @Autowired
    private NocReadCardService nocReadCardService;

    @Autowired
    private NocMemberService nocMemberService;

    @Autowired
    private NocBusinessService nocBusinessService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("noc:nocreadcard:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = nocReadCardService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{readId}")
    @RequiresPermissions("noc:nocreadcard:info")
    public R info(@PathVariable("readId") Integer readId){
        NocReadCardEntity nocReadCard = nocReadCardService.getById(readId);

        return R.ok().put("nocReadCard", nocReadCard);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(HttpServletRequest re){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
        NocReadCardEntity nocReadCard = new NocReadCardEntity();
        nocReadCard.setBarCode(re.getParameter("barCode"));
        nocReadCard.setPcName(re.getParameter("pcName"));
        nocReadCard.setReadDate(sdf1.format(new Date()));
        nocReadCard.setReadTime(sdf2.format(new Date()));
        nocReadCard.setTid(this.getDefaultTemplateId());
        nocReadCardService.save(nocReadCard);
        //启动多线程发送短信
        SendMessage sendMessage = new SendMessage();
        sendMessage.setBarCode(re.getParameter("barCode"));
        Thread thread = new Thread(sendMessage);
        thread.start();

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("noc:nocreadcard:update")
    public R update(@RequestBody NocReadCardEntity nocReadCard){
        ValidatorUtils.validateEntity(nocReadCard);
        nocReadCardService.updateById(nocReadCard);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("noc:nocreadcard:delete")
    public R delete(@RequestBody Integer[] readIds){
        nocReadCardService.removeByIds(Arrays.asList(readIds));

        return R.ok();
    }
    class SendMessage implements Runnable{
        String barCode;
        public void setBarCode(String barCode){
            this.barCode = barCode;
        }
        @Override
        public void run(){
            //发送短信线程启动
            System.out.println("发送线程启动");
            Map<String,Object> map = new HashMap<>();
            map.put("barCode",barCode);
            if(nocMemberService.selectCount(map)!=0){
                nocMemberService.updateSign(map);
            }
            else{
                if(nocBusinessService.selectCount(map)!=0){
                    nocBusinessService.updateSign(map);
                }
            }
            Integer memberId = nocMemberService.getMemberId(barCode);
            if(memberId != 100000){
                /*nocMemberService.sendMemberMsg(memberId);
                nocMemberService.sendMatchMsg(memberId);*/
                nocMemberService.sendDaMemberMsg(memberId);
            }
        }
    }

}
